;; RoFIME: Ring of Favorite Input MEthods

;; Set your favorite input methods
(defvar favorite-input-methods 
      '(nil "korean-hangul3" "japanese" "TeX" "ucs"))
(defvar default-input-method "korean-hangul3")

;; Set key bindings
(global-set-key [?\C-\\] 'toggle-input-method-ring)
(global-set-key [?\S- ] 'toggle-the-other-input-method)
(global-set-key [?\C-|] 'rofime-rotate-the-others)


;; load settings
(cond 
 ((boundp 'favorite-input-methods)
  (setq input-method-ring favorite-input-methods))
 (T (setq input-method-ring ())))
(cond 
 ((boundp 'default-input-method)
  (add-to-list 'input-method-ring default-input-method)))

;; functions
(defun rofime-swap-heads (ring)
  (cons (cadr ring)
	(cons (car ring)
	      (cddr ring))))

(defun rofime-swap-the-ends (ring)
  (append (last ring) 
	  (append (reverse (cdr (reverse (cdr ring)))) 
		  (list (car ring)))))

(defun rofime-reverse (ring)
  (reverse ring))

(defun rofime-rotate (ring)
  (if (< (length ring) 2) ring
    (append (cdr ring) (list (car ring)))))

(defun rofime-add (ime)
  (add-to-list 'input-method-ring ime))


(defun rofime-pop () (interactive)
  (pop input-method-ring)
  (set-input-method (car input-method-ring)))

(defun rofime-message () (interactive)
  (message "%S" input-method-ring))


(defun toggle-the-other-input-method() (interactive)
  (rofime-add current-input-method)
  (setq input-method-ring
	(rofime-swap-heads input-method-ring))
  (set-input-method (car input-method-ring))
  (rofime-message))

(defun toggle-input-method-ring() (interactive)
  (rofime-add current-input-method)
  (if (equal (car input-method-ring) current-input-method)
      (setq input-method-ring 
	    (rofime-rotate input-method-ring)))
  (set-input-method (car input-method-ring))
  (rofime-message))

(defun rofime-rotate-the-others () (interactive)
  (setq input-method-ring
	(cons (car input-method-ring)
	      (rofime-rotate (cdr input-method-ring))))
  (rofime-message))

(provide 'lofime)
